<%@page import="org.apache.taglibs.standard.tag.el.core.ForEachTag"%>
<%@page import="com.conferences.model.User"%>
<%@page import="com.conferences.model.Conference"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>


	<%
		List<Conference> result = new ArrayList<Conference>();
		List<Conference> result2 = new ArrayList<Conference>();
		
	 	User loggedUser = (User) request.getAttribute("loggedUser");
	    List<Conference> conferences = (List<Conference>) request.getAttribute("conferencesAll");
	    
	    for (Conference c: conferences) {
	    	if (c.getAuthor().getId().compareTo(loggedUser.getId()) == 0) {
	    		result2.add(c);
	    		continue;
	    	}
	    	
	    	boolean tmp = true;
	    	for (User u: c.getUsers()) {
	    		
	    		if (u.getId().compareTo(loggedUser.getId()) == 0) {
	    			tmp = false;
	    			break;
	    		}
	    	}
	    	if (tmp == true) {
	    		result.add(c);
	    	} else {
	    		result2.add(c);
	    	}
	    }
	    
	    
	    
	    pageContext.setAttribute("conferences", result);
	    pageContext.setAttribute("conferences2", result2);
	 %>

<t:template>
	    <jsp:attribute name="header" >
		      <div class="row featurette">
		        <div class="col-md-10">
		          <h2 class="featurette-heading">Witaj na portalu <span class="text-muted"> konferencyjnym.</span></h2>
		        </div>
		      </div>
		      <hr class="featurette-divider">
	    </jsp:attribute>
	    <jsp:attribute name="footer">Footer</jsp:attribute>
	    <jsp:body>
    
		<h3>Lista wszystkich konferencji</h3><br/>
		<br/>
		<div class="table-responsive">
		  <table class="table table-hover">
			<thead>
				<tr>
					<th>Data rozpoczęcia</th>
					<th>Temat</th>
					<th>Organizator</th>
					<th>Limit osobowy</th>
					<th>Akcja</th>
				</tr>
			</thead>
			<tbody>
			<c:forEach var="conference" items="${conferences}">
				<tr class='conferenceItemRow' id="${conference.id}">
					<td><fmt:formatDate pattern="dd-MM-YYYY" value="${conference.date}" /></td>
					<td>${conference.subject}</td>
					<td>${conference.author.firstname} ${conference.author.lastname}</td>
					<td style="padding-left:60px">${conference.personLimit}</td>
					<td style="padding-left:60px">
						<a href="<c:url value="/conference/join/${conference.id}.htm" />" class="btn btn-default" role="button">Zapisz się</a>
					</td>
				</tr>
			</c:forEach>
			<c:forEach var="conference" items="${conferences2}">
				<tr class='conferenceItemRow' id="${conference.id}">
					<td><fmt:formatDate pattern="dd-MM-YYYY" value="${conference.date}" /></td>
					<td>${conference.subject}</td>
					<td>${conference.author.firstname} ${conference.author.lastname}</td>
					<td style="padding-left:60px">${conference.personLimit}</td>
					<td style="padding-left:60px">
						Jesteś zapisany
					</td>
				</tr>
			</c:forEach>
			</tbody>
	      </table>
	    </div>
    </jsp:body>
</t:template>